package com.lnu_devteam.zongce.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lnu_devteam.zongce.annotation.AuthCheck;
import com.lnu_devteam.zongce.common.BaseResponse;
import com.lnu_devteam.zongce.common.ErrorCode;
import com.lnu_devteam.zongce.common.PageRequest;
import com.lnu_devteam.zongce.common.ResultUtils;
import com.lnu_devteam.zongce.constant.ScoreConstant;
import com.lnu_devteam.zongce.exception.BusinessException;
import com.lnu_devteam.zongce.model.dto.ScoreDecreaseDTO;
import com.lnu_devteam.zongce.model.dto.ScoreIncreaseDTO;
import com.lnu_devteam.zongce.model.enums.RoleEnum;
import com.lnu_devteam.zongce.model.vo.ScoreVO;
import com.lnu_devteam.zongce.service.ScoreService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author xbaozi
 * @version 1.0
 * @classname ScoreController
 * @date 2023-03-23  15:25
 * @description 分数接口
 */
@Api(value = "/score/apply", tags = {"分数接口"})
@RestController
@Slf4j
@RequestMapping("/score/apply")
public class ScoreController {
    @Resource
    private ScoreService scoreService;

    /**
     * @description 通过填写表单提交减分申请
     * @author xbaozi
     * @param scoreDecreaseDTO  减少分数接口参数
     * @date 2023/4/6 22:54
     **/
    @ApiOperation("通过填写表单提交减分申请")
    @PostMapping("/decrease")
    @AuthCheck(anyRole = {RoleEnum.ADMIN, RoleEnum.SUPER})
    public BaseResponse<Boolean> decreaseScore(@RequestBody ScoreDecreaseDTO scoreDecreaseDTO) {
        if (scoreDecreaseDTO == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean res = scoreService.changeScore(scoreDecreaseDTO, scoreDecreaseDTO.getUAccount(), ScoreConstant.DECREASE_TYPE);
        return ResultUtils.success(res);
    }


    /**
     * @description 通过填写表单提交加分申请
     * @author xbaozi
     * @param scoreIncreaseDTO  增加分数接口参数
     * @date 2023/4/6 22:53
     **/
    @ApiOperation("通过填写表单提交加分申请")
    @PostMapping("/increase")
    @AuthCheck(anyRole = {RoleEnum.ADMIN, RoleEnum.SUPER})
    public BaseResponse<Boolean> increaseScore(@RequestBody ScoreIncreaseDTO scoreIncreaseDTO) {
        if (scoreIncreaseDTO == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean res = scoreService.changeScore(scoreIncreaseDTO, Strings.EMPTY, ScoreConstant.INCREASE_TYPE);
        return ResultUtils.success(res);
    }

    /**
     * @description 通过分数ID撤回分数变更申请
     * @author xbaozi
     * @date 2023/3/27 21:43
     * @param scoreId   分数变更id
     * @return BaseResponse<Boolean>
     **/
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "long", name = "ScoreId", value = "分数变更id", required = true)
    })
    @ApiOperation(value = "通过分数ID撤回分数变更申请", notes = "通过分数ID撤回分数变更申请", httpMethod = "POST")
    @PostMapping("/recall")
    public BaseResponse<Boolean> recallScoreApply(Long scoreId, HttpServletRequest request) {
        if (scoreId <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean res = scoreService.recallScoreApply(scoreId, request);
        return ResultUtils.success(res);
    }

    /**
     * @description 通过分数变更ID审核通过申请
     * @author xbaozi
     * @date 2023/3/26 10:28
     * @param scoreId   分数变更id
     * @return BaseResponse<Boolean>
     **/
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "long", name = "scoreId", value = "分数变更id", required = true)
    })
    @ApiOperation(value = "通过分数变更ID审核通过申请", notes = "通过分数变更ID审核通过申请", httpMethod = "POST")
    @PostMapping("/audit/approved")
    @AuthCheck(anyRole = {RoleEnum.ADMIN, RoleEnum.SUPER})
    public BaseResponse<Boolean> auditApproved(Long scoreId, HttpServletRequest request) {
        if (scoreId == null || scoreId <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean res = scoreService.audit(scoreId, ScoreConstant.AUDIT_APPROVED, ScoreConstant.DEFAULT_REASON, request);
        return ResultUtils.success(res);
    }

    /**
     * @description 通过分数变更ID审核驳回申请
     * @author xbaozi
     * @date 2023/3/26 10:28
     * @param scoreId   分数变更id
     * @param reason    驳回原因
     * @return BaseResponse<Boolean>
     **/
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "long", name = "scoreId", value = "分数变更id", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "reason", value = "驳回原因", required = true)
    })
    @ApiOperation(value = "通过分数变更ID审核驳回申请", notes = "通过分数变更ID审核驳回申请", httpMethod = "POST")
    @PostMapping("/audit/dismissed")
    @AuthCheck(anyRole = {RoleEnum.ADMIN, RoleEnum.SUPER})
    public BaseResponse<Boolean> auditDismissed(Long scoreId, String reason, HttpServletRequest request) {
        if (scoreId == null || scoreId <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean res = scoreService.audit(scoreId, ScoreConstant.AUDIT_DISMISSED, reason, request);
        return ResultUtils.success(res);
    }

    /**
     * @description 分页查询申请记录，管理员/用户可查询所有/个人记录
     * @author xbaozi
     * @date 2023/3/26 13:46
     * @param pageRequest   分页请求参数
     * @param pageType      查询类型，默认-1查询全部(0未审核，1审核通过，2审核驳回)
     * @param scoreType     分数类型，默认-1查询全部(德育分0、技能分1、文体分2)
     * @param changeType    分数变更类型，默认-1查询全部(加分0、减分1)
     **/
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "pageType", value = "查询类型，默认-1查询全部(0未审核，1审核通过，2审核驳回)", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "scoreType", value = "分数类型，默认-1查询全部(德育分0、技能分1、文体分2)", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "changeType", value = "分数变更类型，默认-1查询全部(加分1、减分0)", required = true)
    })
    @ApiOperation(value = "分页查询申请记录，管理员/用户可查询所有/个人记录", notes = "分页查询申请记录，管理员/用户可查询所有/个人记录", httpMethod = "GET")
    @GetMapping("/page")
    public BaseResponse<Page<ScoreVO>> pageScoreApply(PageRequest pageRequest, @RequestParam(defaultValue = "-1") String pageType, @RequestParam(defaultValue = "-1") String scoreType,  @RequestParam(defaultValue = "-1") String changeType) {
        Page<ScoreVO> scorePage = scoreService.pageScoreApply(pageRequest, pageType, scoreType, changeType);
        return ResultUtils.success(scorePage);
    }
}
